import Vue from "vue";
import VueRouter from "vue-router";
// RouterTab 内置路由
import { RouterTabRoutes } from "vue-router-tab";
import utils from "@/utils";
import Frame from "../layout/Frame.vue";
import Login from "../views/Login.vue";

const importPage = (view) => () => import(`../views/${view}/Index.vue`);

Vue.use(VueRouter);

const routes = [
  {
    path: "/Login",
    name: "Login",
    component: Login,
  },
  {
    path: "/",
    component: Frame,
    children: [
      ...RouterTabRoutes,
      {
        path: "/", // 默认页和父级路由一致
        name: "Home",
        component: importPage("Home"),
        meta: {
          title: "首页", // 页签标题
          auth:true
        },
      },
      {
        path: "/Configuration",
        name: "Configuration",
        component: importPage("Configuration"),
        meta: {
          title: "配置管理",
          auth:true
        },
      },
      {
        path: "/Menus",
        name: "Menus",
        component: importPage("Menus"),
        meta: {
          title: "菜单管理",
          auth:true
        },
      },
      {
        path: "/Advertisement",
        name: "Advertisement",
        component: importPage("Advertisement"),
        meta: {
          title: "广告位管理",
          auth:true
        },
      },
      {
        path: "/ThematicManagement",
        name: "ThematicManagement",
        component: importPage("ThematicManagement"),
        meta: {
          title: "专题页管理",
          auth:true
        },
      },
      {
        path: "/User",
        name: "User",
        component: () => import(`../views/User/Wrap`),
        meta: {
          title: "用户管理",
          auth:true
        },
      },
      {
        path: "/Role",
        name: "Role",
        component: importPage("Role"),
        meta: {
          title: "角色管理",
          auth:true
        },
      },
      {
        path: "/News",
        name: "News",
        component: importPage("News"),
        meta: {
          title: "新闻资讯",
          auth:true
        },
      },
      {
        path: "/Knowledge",
        name: "Knowledge",
        component: () => import(`../views/Knowledge/Wrap`),
        meta: {
          title: "知识管理",
          auth:true
        },
      },
      {
        path: "/Policy",
        name: "Policy",
        component: () => import(`../views/HelpManage/Policy`),
        meta: {
          title: "补贴政策",
          auth:true
        },
      },
      {
        path: "/Service",
        name: "Service",
        component: () => import(`../views/HelpManage/Service/Wrap`),
        meta: {
          title: "网约农服",
          auth:true
        },
      },
      {
        path: "/CreditHelp",
        name: "CreditHelp",
        component: importPage("CreditHelp"),
        meta: {
          title: "信贷帮扶",
          auth:true
        },
      },
      {
        path: "/AgricultureProducts",
        name: "AgricultureProducts",
        component: importPage("AgricultureProducts"),
        meta: {
          title: "农产品管理",
          auth:true
        },
      },
      {
        path: "/AnyPhoto",
        name: "AnyPhoto",
        component: () => import(`../views/AnyPhoto/Wrap`),
        meta: {
          title: "随手拍管理",
          auth:true
        },
      },
      {
        path: "/Market",
        name: "Market",
        component: () => import(`../views/Market/Wrap`),
        meta: {
          title: "行情管理",
          auth:true
        },
      },
      {
        path: "/LoginLogs",
        name: "LoginLogs",
        component: importPage("LoginLogs"),
        meta: {
          title: "登录日志",
          auth:true
        },
      },
      {
        path: "/LogisticsDelivery",
        name: "LogisticsDelivery",
        component: () => import(`../views/LogisticsDelivery/Wrap`),
        meta: {
          title: "物流发货",
          auth:true
        },
      },
      {
        path: "/LogisticsReceipt",
        name: "LogisticsReceipt",
        component: importPage("LogisticsReceipt"),
        meta: {
          title: "物流收货",
          auth:true
        },
      },
      {
        path: "/ExpenseManagement",
        name: "ExpenseManagement",
        component: importPage("ExpenseManagement"),
        meta: {
          title: "费用管理",
          auth:true
        },
      },
      {
        path: "/Site",
        name: "Site",
        component: importPage("Site"),
        meta: {
          title: "站点管理",
          auth:true
        },
      },
      {
        path: "/Data",
        name: "Data",
        component: importPage("Data"),
        meta: {
          title: "数据采集",
          auth:true
        },
      },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  routes,
});

router.beforeEach((to, from, next) => {
  let session = utils.getlocalStorage('userInfo') || {}
  if (to.meta.auth) {
    if (session && session.token) {
      if (to.name == 'login') {
        next(false)
      } else {
        next()
      }
    } else {
      next('/login')
    }
  } else {
    if (session && session.token) {
      if (to.name == 'login') {
        next('/')
      } else {
        next()
      }
    } else {
      next()
    }
  }
})

export {routes};

export default router;
